"""
作用：对selenium进行二次封装，供怕个包中的类继承
类名：Base
方法：
    __init__:创建webdriver对象
    get:请求目标网址
    find_element:单个元素定位
    find_elements:多个元素定位
    send_keys:输入内容
    click:点击
    text:获取标签文本

    quit:退出浏览器
"""
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


# 打开浏览器驱动,返回webdriver对象
def get_driver(browser='chrome'):
    if browser == 'chrome':
        return webdriver.Chrome()
    else:
        return webdriver.Firefox()


class Base:

    def __init__(self,driver):
        """
        # 初始化webdriver对象
        """
        self.driver = driver
        sleep(1)


    def get(self, url):
        """
        # 封装访问方法
        :param url:
        :return:
        """
        try:
            self.driver.get(url)
            self.driver.implicitly_wait(4)
        except (InvalidArgumentException, WebDriverException) as e:
            print(e)


    def find_element(self,locator,timeout=5,poll_frequency=0.5):
        """
        # 封装定位单个元素方法
        :param locator:
        :param timeout:
        :param poll_frequency:
        :return:
        """
        try:
            return WebDriverWait(self.driver,timeout,poll_frequency).until(EC.presence_of_element_located(locator),message='单个元素定位错误')
        except (InvalidArgumentException, WebDriverException) as e:
            print(e)

    def find_elements(self,locator,timeout=5,poll_frequency=0.5):
        """
        封装定位多个元素的方法
        :param locator:
        :param timeout:
        :param poll_frequency:
        :return:
        """
        try:
            return WebDriverWait(self.driver,timeout,poll_frequency).until(EC.presence_of_all_elements_located(locator),message='多个元素定位错误')
        except (InvalidArgumentException,WebDriverException) as e:
            print(e)

    def send_keys(self,locator,content):
        """
        封装输入内容方法
        :param locator:
        :param content:
        :return:
        """
        sk_webelement = self.find_element(locator)
        if sk_webelement:
            return sk_webelement.send_keys(content)

    def click(self,locator):
        """
        封装click方法
        :param locator:
        :return:
        """
        cl_webelement =self.find_element(locator)
        if cl_webelement:
            return cl_webelement.click()

    def text(self,locator):
        """
        封装获取标签文本方法
        :param locator:
        :return:
        """
        text_element = self.find_element(locator)
        if text_element:
            return text_element.text

    def quit(self,seconds=3):
        sleep(seconds)
        self.driver.quit()

    def iframe_index(self, locator, index: int = 0):  # 定义一个用索引号进入iframe的方法
        try:
            self.driver.switch_to.frame(index)
        except:
            print(f'没有找到{locator}元素')  # 元素未找到

    def login_act(self):  # 定义ECShop后台管理登录页面的函数
        self.get("http://localhost:8080/ecshop/admin")  # 进入登录后台的页面
        self.find_element((By.XPATH, '//*[@id="panelCloud"]/div[1]/span')).click()  # 点击关掉激活系统页面
        self.find_element((By.ID, 'cloudLogin')).click()  # 进入ecshop登录页面
        self.find_element((By.NAME, 'username')).send_keys('root1234')  # 输入账号
        self.find_element((By.NAME, 'password')).send_keys('root1234')  # 输入密码
        self.find_element((By.CLASS_NAME, 'btn-a')).click()  # 点击登录

    def login_user(self):  # 登录用户端
        self.get("http://localhost:8080/ecshop")  # 请求网址
        self.click((By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[1]'))  # 点击登录
        self.send_keys((By.NAME, 'username'), 'root')  # 输入账号
        self.send_keys((By.NAME, 'password'), 123456)  # 输入密码
        self.click((By.NAME, 'submit'))

    def quit_element_frame(self):  # 退出iframe
        """
        退出iframe
        :return:
        """
        self.driver.switch_to.default_content()

    def alert_accept(self):
        # 系统弹窗处理，确定
        alert = self.driver.switch_to.alert
        alert.accept()

    def alert_dismiss(self):
        # 系统弹窗处理，取消
        alert = self.driver.switch_to.alert
        alert.dismiss()


if __name__ == '__main__':
    # driver = get_driver()
    # case = Base(driver)
    # case.get('https://www.baidu.com')
    # case.find_element((By.ID,'kw'))
    # # case.send_keys((By.ID,'kw'),'天气预报')
    # # case.click((By.ID,'su'))
    # print(case.text((By.LINK_TEXT, '新闻')))
    # case.quit()

    driver = get_driver()
    case = Base(driver)
    case.login_user()

